package gold.digger;

import gold.utils.InputUtil;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC873 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int lenLongestFibSubseq(int[] arr) {
            Map<String, Integer> memo = new HashMap<>();
            Set<Integer> prevSet = new HashSet<>();
            prevSet.add(arr[0]);
            prevSet.add(arr[1]);
            int res = 0;

            for (int i = 2; i < arr.length; i++) {
                for (int j = i - 1; j >= 0; j--) {
                    if (arr[i] - arr[j] < arr[j] && prevSet.contains(arr[i] - arr[j])) {
                        memo.put(arr[j] + "_" + arr[i], Math.max(3, memo.getOrDefault(arr[i] - arr[j] + "_" + arr[j], 0) + 1));
                        res = Math.max(res, memo.get(arr[j] + "_" + arr[i]));
                    }
                }

                prevSet.add(arr[i]);
            }

            return res;
        }
    }

    public void run() {
        Solution solution = new Solution();
//        int[] arr = InputUtil.toIntegerArray("[1,2,3,4,5,6,7,8]");
        int[] arr = InputUtil.toIntegerArray("[1,3,7,11,12,14,18]");
        System.out.println(solution.lenLongestFibSubseq(arr));
    }

    public static void main(String[] args) throws Exception {
        LC873 an = new LC873();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
